package com.trio.gamerental.service.impl;


import com.trio.gamerental.dao.NoteDao;
import com.trio.gamerental.dao.UserDao;
import com.trio.gamerental.model.Note;
import com.trio.gamerental.model.User;
import com.trio.gamerental.pageModel.NoteHelper;
import com.trio.gamerental.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 张俊强~ on 2016/10/26.
 * function:
 */
@Service
public class NoteServiceImpl implements NoteService {
    @Autowired
    private NoteDao noteDao;
    @Autowired
    private UserDao userDao;

    @Override
    public NoteHelper getAllNotes(String page,String row,String userEmail) {
        NoteHelper noteHelper=new NoteHelper();
        List<Note> noteList;
        String hql = "from Note n where n.userId!=2 and n.userId!=1 order by n.date desc";
        if(userEmail.equals("1512769701@qq.com")||userEmail.equals("1256915243@qq.com")){
            hql =  "from Note n order by n.date desc";
        }
        noteList=noteDao.find(hql,Integer.parseInt(page),Integer.parseInt(row));
        if(noteList!=null){
            noteHelper.setNoteList(noteList);
            noteHelper.setMessage("获取成功");
            noteHelper.setSuccess(true);
        }else{
            noteHelper.setNoteList(null);
            noteHelper.setMessage("留言为空");
            noteHelper.setSuccess(false);
        }
        return noteHelper;
    }

    @Override
    public boolean addNote(Note note) {
        Serializable id = noteDao.save(note);
        if(id==null) return false;
        else return true;
    }

    @Override
    public Integer getIdByEmail(String loginEmail) {

        System.out.println(loginEmail);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("loginEmail", loginEmail);
        String hql = "from User u where u.loginEmail=:loginEmail";

        User user = userDao.get(hql, params);
        if (user == null) {
            return null;
        }else{
            return user.getUserId();
        }
    }
}
